﻿/// <reference path="../Scripts/jquery-2.0.3.intellisense.js" />
/// <reference path="../Scripts/angular.js" />

var app = angular.module("app", []);

app.controller("mainCtrl", function ($scope) {
    console.log("");
    console.log("scope from contoller (PARENT SCOPE) " + $scope.$id);

});

app.factory("shareService", function () {
    return {};
});

app.directive("directiveOne", function (shareService) {
    return {
        scope: {
        },
        restrict: "C",
        template: '<div class="dir">' +
                      '<input type="text" ng-model="textInputOne"  />' +
                      '<input type="button" ng-click="alertTextOne()" value="Alert Text From Input" />' +
                      '<p>Directive ONE</p>' +
                  '</dir>',
        link: function (scope, element, attrs) {
            scope.$watch(function () {
                return scope.textInputOne;
            }, function (newVal) {
                shareService.inputVal = newVal;
            });

            console.log("");
            console.log("DIRECTIVE ONE");
            console.log("scope from directive ONE ISOLATED " + scope.$id);
            console.log("parent scope from directive TWO " + scope.$parent.$id);
        },
        controller: function ($scope) {
            $scope.alertTextOne = function () {
                var input = $scope.textInputOne;
                alert(input);
            };


        }
    };
});


app.directive("directiveTwo", function (shareService) {
    return {
        scope: {},
        restrict: "C",
        template: '<div class="dir">' +
                      '<input type="text" ng-model="textInputTwo" value="" />' +
                      '<br />' +
                      '<input type="button" ng-click="alertTextFromOutside()" value="Alert Text From Outside directives" />' +
                      '<br />' +
                      '<input type="button" ng-click="alertTextFromTwo()" value="Alert Text From TWO" />' +
                      '<br />' +
                      '<input type="button" ng-click="alertTextFromONE()" value="Alert Text From ONE" />' +
                      '<br />' +
                      '<p>Directive TWO</p>' +
                      '<p>{{sharedValue}}</p>' +
                  '</dir>',
        link: function (scope) {
            scope.$watch(function () {
                return shareService.inputVal;
            }, function (newVal) {
                scope.sharedValue = newVal;
            });

            console.log("");
            console.log("DIRECTIVE TWO");
            console.log("scope from directive TWO ISOLATED " + scope.$id);
            console.log("parent scope from directive TWO " + scope.$parent.$id);
        },
        controller: function ($scope) {

            $scope.alertTextFromOutside = function () {
                console.log("");
                console.log("parent scope from directive TWO  in function " + $scope.$parent.$id);
                console.log("scope from directive TWO  in function " + $scope.$id);
                var input = $scope.$parent.parentModel;
                alert(input);
            };

            $scope.alertTextFromTwo = function () {
                console.log("");
                console.log("parent scope from directive TWO  in function " + $scope.$parent.$id);
                console.log("scope from directive TWO  in function " + $scope.$id);
                var input = $scope.textInputTwo;
                alert(input);
            };

            $scope.alertTextFromONE = function () {
                alert(shareService.inputVal);
                console.log("");
                console.log("parent scope from directive TWO  in function " + $scope.$parent.$id);
                console.log("scope from directive TWO  in function " + $scope.$id);
            };
        }
    };
});